K8S |
您所在的位置:网站首页 › k8s service配置负载均衡策略 › K8S |
在Micro Service的治理中。 有两个很重要的点, 集群外部的用户/service 如何访问集群内的 入口服务(例如UI service)集群内的service A 如何 访问 集群内的service B为什么有上面的问题 无非是: 集群内的service 都是多实例的每个service 实例都有单独不同的ip如何负载均衡?如图: 用spring cloud gateway 来反向代理集群内的对外service, 例如图中的Service A, 如果其他Service 没有被配置在gateway中, 集群外部是无法直接访问的, 更加安全。 通常这个api gateway所在的server 具有双网卡, 1个ip在外网, 1个ip在集群内网 同是Spring Cloud Gateway 自带Load balancer 功能(基于 Spring cloud loadbalancer) , 所以即使要exposed 的service 有多个实例, Gateway同样可以根据指定规则 分发到不同的instance. 集群内 Service A to Service B 使用Eureke 作为注册中心, 每个service 的instance 都要往里面注册, 以给每个service 的多个instance 获得1个common的service Name作为DNS使用Ribbon(继承在Eureka) 中, 作为load balancer 进行request转发如图: K8S 的service 包括了很多种, ingress, nodeport, clusterIp, externalName 都是属于service的 集群外 to 集群内使用ingress or NodePort 来作为纵向流量代理, 而ingress 和 NodePort 都是自带load balancer 的。 置于什么是纵向横向流量 参考 使用ClusterIP 作为 Service B之的反向代理, ClusterIP 的service自带loadbalancer 功能, 这样Service A就可以通过ClusterIP service的名字DNS 来访问Service B了 虽然k8s 没用Eureka, Nacos等注册中心, 但是实际上k8s 的service list 实际上就是1个注册中心了! 原理如图: 总的来说,ClusterIP 是 Kubernetes 集群内部的一种服务发现和负载均衡机制,用于实现集群内部的内部通信和服务访问。它提供了一个虚拟 IP 地址给 Service,并通过负载均衡算法将请求分发给关联的后端 Pod。ClusterIP 适用于内部服务,不直接对外部公开。 NodePort 和 ClusterIP 的具体例子解下来我会用 NodePort 和 ClusterIP 来demo 以下 k8s service A 如何 访问 ServiceB 置于从集群外访问为何不用ingress, 是因为k8s 的博文系列还没提到Ingress. 大概框架 在这个例子中 我们会部署: Service A: bq-api-service Service B: cloud-user nodePort service: nodeport-bq-api-service clusterIP service: clusterip-cloud-user 置于这里两个service 具体是什么不重要, 可以认为它们是两个简单的springboot service 并没有集成任何spring cloud 的框架。 cleanup当前k8s 环境是干净的 [gateman@manjaro-x13 bq-api-service]$ kubectl get all -o wide NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR service/kubernetes ClusterIP 10.96.0.1 443/TCP 77d 部署 Service B , cloud-user service 更新info 接口让其return hostname先update /actuator/info 接口 让其可以return 当前service 所在server/container 的hostname @Component @Slf4j public class AppVersionInfo implements InfoContributor { @Value("${pom.version}") // https://stackoverflow.com/questions/3697449/retrieve-version-from-maven-pom-xml-in-code private String appVersion; @Autowired private String hostname; @Value("${spring.datasource.url}") private String dbUrl; @Override public void contribute(Info.Builder builder) { log.info("AppVersionInfo: contribute ..."); builder.withDetail("app", "Cloud User API") .withDetail("version", appVersion) .withDetail("hostname",hostname) .withDetail("dbUrl", dbUrl) .withDetail("description", "This is a simple Spring Boot application to demonstrate the use of BigQuery in GCP."); } }测试效果: [gateman@manjaro-x13 bq-api-service]$ curl 127.0.0.1:8080/actuator/info {"app":"Cloud User API","version":"0.0.1","hostname":"manjaro-x13","dbUrl":"jdbc:mysql://34.39.2.90:6033/demo_cloud_user?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true","description":"This is a simple Spring Boot application to demonstrate the use of BigQuery in GCP."} [gateman@manjaro-x13 bq-api-service]$ 利用cloudbuild 和 其trigger 让其自动部署docker image 到GAR (google artifact repository)cloudbuild-gar.yaml # just to update the docker image to GAR with the pom.xml version steps: - id: run maven install name: maven:3.9.6-sapmachine-17 # https://hub.docker.com/_/maven entrypoint: bash args: - '-c' - | whoami set -x pwd mvn install cat pom.xml | grep -m 1 "" | sed -e 's/.*\([^ |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |